<?php

namespace app\api\service;

use app\common\model\OrderItem;
use think\Log;

class OrderItemService extends BaseService
{
    public function submit($orderId, $dataId, $dataType, $name, $qty, $price, $discountAmount, $content, $remark)
    {
        $orderItem = new OrderItem();
        $data = [
            'order_id' => $orderId,
            'data_id' => $dataId,
            'data_type' => $dataType,
            'name' => $name,
            'qty' => $qty,
            'unit_amount' => $price,
            'discount_amount' => $discountAmount,
            'total_amount' => bcsub(bcmul($qty, $price, 2), $discountAmount, 2),
            'content' => $content,
            'remark' => $remark,
        ];

        $result = $orderItem->data($data)->save();
        if (empty($result)) {
            $this->setMsg('订单项写入失败！');
            Log::write('订单项写入失败！' . $result, 'error');
            return false;
        }
        $orderItem['content'] = json_decode($orderItem['content'], true);
        return $orderItem;
    }
}
